/**
 * 
 */
package org.gnf.genewiki.associations.analysis;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.gnf.genewiki.Config;
import org.gnf.genewiki.associations.CandiAnnoSorter;
import org.gnf.genewiki.associations.CandidateAnnotation;
import org.gnf.genewiki.associations.CandidateAnnotations;
import org.gnf.go.Annotation;
import org.gnf.go.GOterm;
import org.gnf.util.Gene;

/**
 * @author bgood
 *
 */
public class PerGene {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		getNumAnnosPerGene();

	}

	/**
	 * Reads gene2pubmed file from NCBI Gene and returns the human genes-pmids
	 * taxon, gene, pmid 	
	 * @param gene2pubmed
	 * @return
	 */
	public static Map<String, Set<String>> getGene2Pmid(String gene2pubmed){
		Map<String, Set<String>> gene2pmid = new HashMap<String, Set<String>>();
		BufferedReader f;
		try {
			f = new BufferedReader(new FileReader(gene2pubmed));
			String line = f.readLine();
			line = f.readLine(); //skip header
			while(line!=null){
				String[] item = line.split("\t");
				if(item[0].equals("9606")){
					Set<String> pmids = gene2pmid.get(item[1]);
					if(pmids==null){
						pmids = new HashSet<String>();
					}
					pmids.add(item[2]);
					gene2pmid.put(item[1], pmids);
				}
				line = f.readLine();
			}
			f.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return gene2pmid;
	}

	public static void getNumAnnosPerGene(){
		CandidateAnnotations cannolist = new CandidateAnnotations();
		cannolist.loadAndFilterCandidateGOAnnotations(Config.merged_mined_annos);
		List<CandidateAnnotation> cannos = cannolist.getCannos();
		cannos = CandiAnnoSorter.setKnownHumanAnnotationsGO(cannos);
		System.out.println("Loaded candidate GO annotations "+cannos.size());		

		//annos from GOA
		Map<String, Set<GOterm>> goa = Annotation.readGene2GO(Config.gene_go_file, true);

		//counts of pmids
		Map<String, Set<String>> gene2pmids = getGene2Pmid(Config.gwroot+"/tmp/gene2pubmed");
		
		Set<String> geneids = new HashSet<String>();
		geneids.addAll(goa.keySet());
		geneids.addAll(gene2pmids.keySet());
		
		HashMap<String,Set<CandidateAnnotation>> gwiki = new HashMap<String, Set<CandidateAnnotation>>();

		for(CandidateAnnotation canno : cannos){
			String gene = canno.getEntrez_gene_id();
			Set<CandidateAnnotation> GOs = gwiki.get(gene);
			if(GOs == null){
				GOs = new HashSet<CandidateAnnotation>();
				gwiki.put(gene, GOs);
			}
			GOs.add(canno);//the text term for the go id		
			gwiki.put(gene, GOs);
		}
		geneids.addAll(gwiki.keySet());

		try {
			FileWriter out = new FileWriter(Config.gwroot+"/tmp/interesting_genes.txt");
			out.write("gene\tpmids\tgoas\tnew_gwikis\trediscovered_gwikis\n");
			for(String gene : geneids){
				Set<GOterm> goas = goa.get(gene);
				int goa_count = 0; int gwiki_new_count = 0; int gwiki_old_count = 0;
				int pmid_count = 0;
				Set<String> pmids = gene2pmids.get(gene);
				if(pmids!=null){
					pmid_count = pmids.size();
				}
				
				if(goas!=null){
					goa_count = goas.size();
				}
				Set<CandidateAnnotation> gwikis = gwiki.get(gene);
				if(gwikis!=null){
					for(CandidateAnnotation canno : gwiki.get(gene)){
						if(canno.getEvidence().isMatches_existing_annotation_directly()){
							gwiki_old_count++;
						}else if(!canno.getEvidence().isMatches_parent_of_existing_annotation()){
							gwiki_new_count++;
						}
					}
					
				}
				if(goa_count<6&&gwiki_new_count>20){
					out.write("gene:"+gene+"\t-"+pmid_count+"\t"+goa_count+"\t"+gwiki_new_count+"\t"+gwiki_old_count+"\n");
				}
			}
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
